//
//  QBVCMe.m
//  QiuBai
//
//  Created by yaohongchao on 16/4/17.
//  Copyright © 2016年 yaohongchao. All rights reserved.
//

#import "QBVCMe.h"
#import "QBVMeCell.h"

@interface QBVCMe ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak  ) UITableView    *tableView;
@property (nonatomic,strong) NSMutableArray *mArr;

@end

@implementation QBVCMe

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupData];
    [self setui];
    // Do any additional setup after loading the view.
}

-(void)setui{
    [self.navigationItem setTitle:@"我"];
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
    self.tableView = tableView;
    WS(weakSelf);
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithKey(CellSelectedBg);
}


-(void)setupData{
//    @"",@"",@""
    self.mArr = [NSMutableArray arrayWithObjects:@[@""],
                 @[@{@"title":@"管理我的糗事",@"imageName":@"qb_me_posted"}],
                 @[@{@"title":@"我的糗友/群",@"imageName":@"qb_me_myfriends"}],
                 @[@{@"title":@"夜间模式",@"imageName":@"qb_me_nightmode"},
                 @{@"title":@"设置",@"imageName":@"qb_me_setting"}],nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.tableView = nil;
}

#pragma mark UITableViewDelegate UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.mArr arrayWithIndex:section].count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return [QBVMeLoginCell getCellHeight];
    }
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0 && indexPath.section == 0){
        QBVMeLoginCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentifierOne];
        if(!cell){
            cell = [[QBVMeLoginCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ReuseIdentifierOne];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.imgV.image = [UIImage imageNamed:@"default_avatar"];
        cell.lblTitle.font = [UIFont systemFontOfSize:18];
        cell.lblTitle.textColor = cMainColor;
        cell.lblTitle.text = @"登录/注册";
        
        cell.lblSubTitle.font = [UIFont systemFontOfSize:14];
        cell.lblSubTitle.textColor = cSubMainColor;
        cell.lblSubTitle.text = @"您可以使用QQ、微信、或微博号";
        cell.dk_backgroundColorPicker = DKColorPickerWithKey(CellBg);

        return cell;
    }else{
        UITableViewCell *cell;
        if(indexPath.section == 3 && indexPath.row == 0){
            cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentifierTwo];
            if(!cell){
                cell = [[QBVMeSwitchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ReuseIdentifierTwo];
                [((QBVMeSwitchCell*)cell).sw addTarget:self action:@selector(nightModeClick:) forControlEvents:UIControlEventTouchUpInside];
            }
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            ((QBVMeSwitchCell*)cell).sw.on = [userDefaults boolForKey:isNightMode];
        }else{
            cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentifierThree];
            if(!cell){
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ReuseIdentifierThree];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
        NSDictionary *dict = [[self.mArr arrayWithIndex:indexPath.section] objectWithIndex:indexPath.row];
        NSString *imageName = [dict objectForKey:@"imageName"];
        NSString *title = [dict objectForKey:@"title"];
        
        cell.imageView.image = [UIImage imageNamed:imageName];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = cMainColor;
        cell.textLabel.text = title;
        cell.dk_backgroundColorPicker = DKColorPickerWithKey(CellBg);
        cell.dk_tintColorPicker = DKColorPickerWithKey(CellSelectedBg);
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)nightModeClick:(UISwitch*)sw{
    if(sw.isOn){
        [[DKNightVersionManager sharedManager]nightFalling];
    }else{
        [[DKNightVersionManager sharedManager] dawnComing];
    }
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:sw.isOn forKey:isNightMode];
}


@end
